/*
 * Copyright (c) 2014. kupat Corporation. All rights reserved.
 *  see statement on http://www.kupat.cn.
 */
package com.kfgj.hkp.common.service;

import com.kfgj.hkp.common.exception.ExceptionEvent;
import com.kfgj.hkp.common.logging.Logger;
import com.kfgj.hkp.common.logging.LoggerFactory;
import com.kfgj.hkp.common.utils.ExceptionUtils;

/**
 * Created by liwensihan on 2014/11/5.
 * <p/>
 * 服务抽象实现支持类，包括一些成功或者错误的设置等等
 *
 * @author liwensihan
 */
public abstract class AbstractService implements IService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final static String SUCCESS_MESSAGE = "操作成功!";

    protected IServiceResponse success(AbstractServiceResponse response) {
        return this.success(response, SUCCESS_MESSAGE);

    }

    protected IServiceResponse success(AbstractServiceResponse response, String message) {
        response.setIsSuccess(true);
        response.setMessage(message);
        response.setCode(ExceptionEvent.successCode);
        return response;
    }

    protected IServiceResponse exception(AbstractServiceResponse response, Throwable cause) {
        return this.exception(response,cause.getMessage(),cause);
    }

    protected IServiceResponse exception(AbstractServiceResponse response, String message, Throwable cause){
        response.setIsException(true);
        response.setIsSuccess(false);
        response.setMessage(message);
        response.setCode(ExceptionEvent.exceptionCode);
        response.setStackTrace(ExceptionUtils.getStackTrace(cause));
        return response;
    }

    protected IServiceResponse error(AbstractServiceResponse response, String message){
        response.setCode(ExceptionEvent.commonErrorCode);
        response.setIsSuccess(false);
        response.setIsException(false);
        response.setMessage(message);
        return response;
    }
    protected IServiceResponse error(AbstractServiceResponse response, String code, String message){
        response.setCode(code);
        response.setIsSuccess(false);
        response.setIsException(false);
        response.setMessage(message);
        return response;
    }
}
